import request from "../utils/request";
import {reactive} from "vue";

/**
 * 公共常量，比如护士站id
 */
const STATION = reactive({
    'id': localStorage.getItem('id'),
    'sno': localStorage.getItem('sno'),
    'uuid': localStorage.getItem('uuid'),
    'ip': localStorage.getItem('ip') === null ?
        '...' :
        localStorage.getItem('ip')
})
const SERVER = reactive({
    'api': {
        'ip': localStorage.getItem('api_server') === null ?
            '...' :
            JSON.parse(localStorage.getItem('api_server')).ip,
        'port': localStorage.getItem('api_server') === null ?
            '' :
            JSON.parse(localStorage.getItem('api_server')).port
    },
    'call': {
        'ip': localStorage.getItem('call_server') === null ?
            '...' :
            JSON.parse(localStorage.getItem('call_server')).ip,
        'port': localStorage.getItem('call_server') === null ?
            '' :
            JSON.parse(localStorage.getItem('call_server')).port
    }
});

/**
 * 护士站id
 */
const ID = {
    'id': STATION.id
};

/**
 * 公共get请求函数
 */
const get = (api, params, headers) => {
    return request({
        url: api,
        method: 'get',
        params: params,
        headers: headers
    });
};

/**
 * 公共post请求函数
 */
const post = (api, form, headers) => {
    return request({
        url: api,
        method: 'post',
        data: form,
        headers: headers
    })
}

/**
 * 根据指定键名（相同键值）进行分组
 */
const groupByKey = (list, key) => {
    let groups = {};
    list.forEach( function(item) {
        let value = JSON.stringify(item[key]);
        groups[value] = groups[value] || [];
        groups[value].push(item);
    });
    return Object.values(groups);
}

export {
    STATION,
    SERVER,
    ID,
    get,
    post,
    groupByKey
}